package com.ray.ioc.core;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.ray.ioc.R;

import java.lang.reflect.Field;

/**
 * 用于获取控件属性的Factory
 * Created by Ray on 2015/9/11.
 */
public class IOCFactory implements LayoutInflater.Factory{
    private final LayoutInflater.Factory factory;
    private IOCLayoutInflater inflater;
    private final Object ioc;
    private static final String[] sClassPrefixList = {
            "android.widget.",
            "android.webkit.",
            "android.view."
    };

    public IOCFactory(IOCLayoutInflater inflater, LayoutInflater.Factory factory,
                      Object ioc){
        this.inflater = inflater;
        this.factory = factory;
        this.ioc = ioc;
    }
    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = null;
        if (context instanceof LayoutInflater.Factory) {
            view = ((LayoutInflater.Factory) context).onCreateView(name, context, attrs);
        }

        if (factory != null && view == null) {
            view = factory.onCreateView(name, context, attrs);
        }
        if (view == null) {
            view = createViewOrFailQuietly(name, context, attrs);
        }

        if (view != null) {
            onViewCreated(view, context, attrs);
        }
        return view;
    }

    protected View createViewOrFailQuietly(String name, Context context, AttributeSet attrs) {
        if (name.contains(".")) {
            return createViewOrFailQuietly(name, null, context, attrs);
        }

        for (final String prefix : sClassPrefixList) {
            final View view = createViewOrFailQuietly(name, prefix, context, attrs);

            if (view != null) {
                return view;
            }
        }

        return null;
    }

    protected View createViewOrFailQuietly(String name, String prefix, Context context,
                                           AttributeSet attrs) {
        try {
            return inflater.createView(name, prefix, attrs);
        } catch (Exception ignore) {
            return null;
        }
    }

    protected void onViewCreated(View view, Context context, AttributeSet attrs) {

        int[] attrIds = { R.attr.bean ,R.attr.click};

        TypedArray a = context.obtainStyledAttributes(attrs, attrIds);

        if (a != null) {
            if (a.length() > 0) {
                String bean = a.getString(0);

                boolean click = a.getBoolean(1,false);
                try {
                    if(bean != null) {
                        Field field = ioc.getClass().getField(bean);
                        field.set(ioc, view);
                    }
                    if(click) {
                        view.getClass().getMethod("setOnClickListener", View.OnClickListener.class).
                                invoke(view,ioc);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            a.recycle();
        }
    }

    public Object getBaseIOC(){
        return ioc;
    }
}
